package com.maochd.cloud.common.core.utils.user;

import com.maochd.cloud.common.core.constant.SecurityConstants;
import com.maochd.cloud.common.core.domain.UserInfo;
import com.maochd.cloud.common.core.exception.BaseException;
import com.maochd.cloud.common.core.utils.request.RequestUtil;

import java.util.Objects;

public class UserUtil {

    /**
     * 获取用户信息
     *
     * @return userInfo
     */
    public static UserInfo userInfo() {
        return RequestUtil.header(SecurityConstants.USER_INFO, UserInfo.class);
    }

    /**
     * 获取用户ID
     *
     * @return userId
     */
    public static Long userId() {
        UserInfo userInfo = userInfo();
        if (Objects.isNull(userInfo)) {
            throw new BaseException("当前用户不存在");
        }
        return userInfo.getId();
    }

    /**
     * 获取用户名称
     *
     * @return userId
     */
    public static String userName() {
        UserInfo userInfo = userInfo();
        if (Objects.isNull(userInfo)) {
            throw new BaseException("当前用户不存在");
        }
        return userInfo.getUsername();
    }

}
